import { reqGetGoodInfo, reqAddorUpdateShopCart } from '@/axios/api/api.js'

export default {
  namespaced: true,
  state: {
    goodInfo: {},
  },
  mutations: {
    // 存储商品详情
    GETGOODINFO(state, res) {
      state.goodInfo = res
    }
  },
  actions: {
    // 拿商品详情
    async getGoodInfo({ commit }, id) {
      // console.log(id);
      const res = await reqGetGoodInfo(id)
      if (res.code == 200) {
        commit('GETGOODINFO', res.data)
      }
    },
    // 加入商品到购物车或修改商品数量的请求
    async AddorUpdateShopCart({ commit }, { detailId, skuNum }) {
      let res = await reqAddorUpdateShopCart(detailId, skuNum)
      if (res.code == 200) {
        return Promise.resolve('OK')
      } else {
        return Promise.reject(new Error('faile'))
      }
    }
  },
  getters: {
    // 路径导航简化的数据
    categoryView(state) {
      return state.goodInfo.categoryView || {}
    },
    // 简化产品信息的数据
    skuInfo(state) {
      return state.goodInfo.skuInfo || {}
    },
    // 产品售卖属性的简化
    spuSaleAttrList(state) {
      return state.goodInfo.spuSaleAttrList || []
    }
  }
}